<?php
namespace Extend\Controller;

/**
 * 同步到锁掌柜相关rpc接口
 * @modify paddy 2016-12-27 创建
 */
class SyncController extends ParentController {
    protected function _initialize() {
        $this->validateUtil = new \Org\Util\Validate();
    }

    /**
     * 验证用户密码并返回用户信息
     * @modify paddy 2016-12-27 创建
     */
    public function verifyUser($data) {
        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }

        $validateRule = array(
            'mobile' => 'notnull,notempty',
            'password' => 'notnull,notempty',
            'areaCode' => 'notnull,notempty',
        );
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }
        
        $mobile = $data['mobile'];
        $areaCode = $data['areaCode'];
        $password = $data['password'];
        
        $adminLoginLogic = A('Common/adminLogin', 'Logic');
        $hashPassword = hash('sha256', $password);
        $adminLogin = $adminLoginLogic->login($mobile, $hashPassword, false, $areaCode);

        $result = array();
        if(is_array($adminLogin)){
            $result['passed'] = true;
            $result['user'] = $adminLogin;
        }else {
        
            $result['passed'] = false;
            $result['errno'] = $adminLogin;
        }

        return parent::returnJson(200, 200, $result);
    }

    /**
     * 获取可同步客栈
     * @modify paddy 2016-12-28 创建
     */
    public function getHotel($data)
    {
        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }
        $validateRule = array(
            'uid' => 'notnull,notempty,id'
        );
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }
        $uid = $data['uid'];
        $hotelLogic = A('Common/hotel', 'Logic');
        $hotels = $hotelLogic->getHotelByUser($uid);
        foreach ($hotels as $k => $v) {
            if ($v['role'] != 1 || $v['status'] != 0) {
                unset($hotels[$k]);
            }
        }   
        return parent::returnJson(200, 200, array_merge($hotels));
    }
    
    /**
     * 获取可同步子账号
     * @modify paddy 2016-12-30 创建
     */
    public function getSubAccount($data)
    {
        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }
        $validateRule = array(
            'uid' => 'notnull,notempty,id'
        );
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }
        $uid = $data['uid'];
        $accountLogic = A('Common/Account', 'Logic');
        $accounts = $accountLogic->getSub($uid);
        return parent::returnJson(200, 200, $accounts);
    }
    /**
     * 获取可同步房间
     * @modify paddy 2016-1-4 创建
     */
    public function getRoom($data)
    {
        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }
        $validateRule = array(
            'uid' => 'notnull,notempty,id',
            'hotelId' => 'notnull,notempty,id'
        );
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }
        $uid = $data['uid'];
        $hotelId = $data['hotelId'];
        
        // 房型和房间号
        $roomTypeLogic = A('Common/RoomType', 'Logic');
        $roomInfo = $roomTypeLogic->getInfoByHotel($hotelId);
        
        // 判断是否需要根据房间权限筛选房间信息
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        if (!$adminAuthorityLogic->checkIsAdmin($uid, $hotelId)) {
            $accountLogic = A('Common/Account', 'Logic');
            $roomAuthority = $accountLogic->getAdminRoomAuthority($uid, $hotelId);
            foreach ($roomInfo as $index => &$roomTypeItem) {
                foreach ($roomTypeItem['room'] as $roomIndex => $roomItem) {
                    if (!in_array($roomItem['id'], $roomAuthority)) {
                        unset($roomTypeItem['room'][$roomIndex]);
                    }
                }
                unset($roomItem);
        
                if (empty($roomTypeItem['room'])) {
                    unset($roomInfo[$index]);
                } else {
                    $roomTypeItem['room'] = array_values($roomTypeItem['room']);
                }
            }
            unset($roomTypeItem);
        }        
        return parent::returnJson(200, 200, $roomInfo);
    }  

    /**
     * 同步录入表
     * @modify zhangzhen 2017-1-14    创建
     */
    public function hotelSyn($data) {
        if (isset($data['sync_notify'][0]['hotel'])) {
            parent::switchTimeUtc(array('hotel' => $data['sync_notify'][0]['hotel']));
        }
        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }

        $hotelEntityModel = D('HotelEntity');
        $hotelSynModel = D('HotelSyn');
        foreach ($data['sync_notify'] as $key => $value) {
            $result = $hotelEntityModel->saveInfo($value['hotel'], null, null, null, $value['communityNo']);
            if (empty($result)) {
                return parent::returnJson(500, 506);
            }
            $result = $addResult = $hotelSynModel->addInfo($value['hotel'],$value['communityNo'],$value['masterentityId'],$value['userId']);
            if (empty($result)) {
                return parent::returnJson(500, 508);
            }
        }
        return parent::returnJson(200, 200, 'success');
    }
    /**
     * 修改卡类型
     * @modify zhangzhen 2017-1-16    创建
     */
    public function changeLocktype($data) {
        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }

        $communityId = $data['communityId'];
        $roomId = $data['roomId'];
        $roomNo = $data['roomNo'];
        $locktype = $data['lockType'];
        $roomSynModel = D('RoomSyn');

        $roomSyn = $roomSynModel->getInfoByMasterRoomId($roomId);
        if($roomSyn){
            $roomInfoModel = D('RoomInfo');
            $roomInfo = $roomInfoModel->getInfo($roomSyn['roominfo_id']);
            if($roomInfo['name'] != $roomNo){
                $roomSynModel->deleteInfo($roomSyn['roominfo_id']);
                $roomInfoModel->editInfo($roomSyn['hotelentity_id'],$roomSyn['roominfo_id'],null,0);
            }else{
                $roomInfoModel->editInfo($roomSyn['hotelentity_id'],$roomSyn['roominfo_id'],null,$locktype);
            }
        }
        return parent::returnJson(200, 200, 'success');
    }

    /**
     * 记录卡获取账户信息
     * @modify zhangzhen 2017-9-1    创建
     */
    public function getInfo($data) {
        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }

        $userId = $data['userId'];
        $userInfo = D('AdminLogin')->field('name,areacode,mobile')->where(['id'=>$userId])->find();
        return parent::returnJson(200, 200, $userInfo);
    }
}
